<?php

namespace app\admin\controller\data;

use app\admin\model\DataStoreBalanceLog;
use app\admin\model\DataStoreLottery;
use app\admin\model\DataStoreUser;
use app\admin\model\DataStoreUserType;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="data_store_user")
 */
class StoreUser extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $store_type = DataStoreUserType::list();
        $this->model = new DataStoreUser();
        $this->assign('store_type', $store_type);

    }



    /**
     * @NodeAnnotation(title="审核")
     */
    public function audit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];

            unset($post['store_type']);
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
                if($post['status'] == 1){
                    DataStoreLottery::create(['store_id'=>$row['id']]);
                }
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }


    /**
     * 充值
     * @return mixed
     */
    public function balance(){
        if($this->request->isGet()) {
         $get = $this->request->get();
         $this->assign('store_id', $get['id']);
        } else {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
            $row = new DataStoreBalanceLog();
            $post['pay_type']= 0;
            $post['real_price']= $post['price'];
            $post['admin_id']=session('admin')['id'];
            $save = $row->save($post);
            } catch (\Exception $e) {
            $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');

        }

        return $this->fetch();
    }


    public function lottery($id){
        $row = DataStoreLottery::where(['store_id'=>$id])->find();
        empty($row) && $this->error('数据不存在');
        if($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');

        }
        $this->assign('row', $row);
        return $this->fetch();
    }


    public function status()
    {
        $status = [
            [
                'id' => '-1',
                'title' => '拒绝'
            ],
            [
                'id' => '1',
                'title' => '审核通过'
            ]
        ];
        $this->success('获取成功', $status);
    }


}